home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume24 / gnudiff1.15 / part07 < prev    next >
Encoding:
Internet Message Format  |  1991-03-05  |  47.6 KB

  1. Subject:  v24i022:  GNU Diff, version 1.15, Part07/08
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 2c556c4c fea50fbc a11aead6 9fdd1991
  5.  
  6. Submitted-by: Paul Eggert <eggert@twinsun.com>
  7. Posting-number: Volume 24, Issue 22
  8. Archive-name: gnudiff1.15/part07
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then unpack
  12. # it by saving it into a file and typing "sh file".  To overwrite existing
  13. # files, type "sh file -c".  You can also feed this as standard input via
  14. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  15. # will see the following message at the end:
  16. #        "End of archive 7 (of 8)."
  17. # Contents:  regex.c1
  18. # Wrapped by eggert@ata on Mon Jan  7 11:25:31 1991
  19. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  20. if test -f 'regex.c1' -a "${1}" != "-c" ; then 
  21.   echo shar: Will not clobber existing file \"'regex.c1'\"
  22. else
  23. echo shar: Extracting \"'regex.c1'\" \(45472 characters\)
  24. sed "s/^X//" >'regex.c1' <<'END_OF_FILE'
  25. X/* Extended regular expression matching and search library.
  26. X   Copyright (C) 1985, 1989-90 Free Software Foundation, Inc.
  27. X
  28. X   This program is free software; you can redistribute it and/or modify
  29. X   it under the terms of the GNU General Public License as published by
  30. X   the Free Software Foundation; either version 1, or (at your option)
  31. X   any later version.
  32. X
  33. X   This program is distributed in the hope that it will be useful,
  34. X   but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. X   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  36. X   GNU General Public License for more details.
  37. X
  38. X   You should have received a copy of the GNU General Public License
  39. X   along with this program; if not, write to the Free Software
  40. X   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  41. X
  42. X
  43. X/* To test, compile with -Dtest.  This Dtestable feature turns this into
  44. X   a self-contained program which reads a pattern, describes how it
  45. X   compiles, then reads a string and searches for it.
  46. X   
  47. X   On the other hand, if you compile with both -Dtest and -Dcanned you
  48. X   can run some tests we've already thought of.  */
  49. X
  50. X
  51. X#ifdef emacs
  52. X
  53. X/* The `emacs' switch turns on certain special matching commands
  54. X  that make sense only in emacs. */
  55. X
  56. X#include "config.h"
  57. X#include "lisp.h"
  58. X#include "buffer.h"
  59. X#include "syntax.h"
  60. X
  61. X#else  /* not emacs */
  62. X
  63. X#if defined (USG) || defined (STDC_HEADERS)
  64. X#ifndef BSTRING
  65. X#include <string.h>
  66. X#define bcopy(s,d,n)    memcpy((d),(s),(n))
  67. X#define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  68. X#define bzero(s,n)    memset((s),0,(n))
  69. X#endif
  70. X#endif
  71. X
  72. X#ifdef STDC_HEADERS
  73. X#include <stdlib.h>
  74. X#else
  75. Xchar *malloc ();
  76. Xchar *realloc ();
  77. X#endif
  78. X
  79. X/* Make alloca work the best possible way.  */
  80. X#ifdef __GNUC__
  81. X#define alloca __builtin_alloca
  82. X#else
  83. X#ifdef sparc
  84. X#include <alloca.h>
  85. X#else
  86. Xchar *alloca ();
  87. X#endif
  88. X#endif
  89. X
  90. X
  91. X/* Define the syntax stuff, so we can do the \<, \>, etc.  */
  92. X
  93. X/* This must be nonzero for the wordchar and notwordchar pattern
  94. X   commands in re_match_2.  */
  95. X#ifndef Sword 
  96. X#define Sword 1
  97. X#endif
  98. X
  99. X#define SYNTAX(c) re_syntax_table[c]
  100. X
  101. X
  102. X#ifdef SYNTAX_TABLE
  103. X
  104. Xchar *re_syntax_table;
  105. X
  106. X#else /* not SYNTAX_TABLE */
  107. X
  108. Xstatic char re_syntax_table[256];
  109. X
  110. X
  111. Xstatic void
  112. Xinit_syntax_once ()
  113. X{
  114. X   register int c;
  115. X   static int done = 0;
  116. X
  117. X   if (done)
  118. X     return;
  119. X
  120. X   bzero (re_syntax_table, sizeof re_syntax_table);
  121. X
  122. X   for (c = 'a'; c <= 'z'; c++)
  123. X     re_syntax_table[c] = Sword;
  124. X
  125. X   for (c = 'A'; c <= 'Z'; c++)
  126. X     re_syntax_table[c] = Sword;
  127. X
  128. X   for (c = '0'; c <= '9'; c++)
  129. X     re_syntax_table[c] = Sword;
  130. X
  131. X   done = 1;
  132. X}
  133. X
  134. X#endif /* SYNTAX_TABLE */
  135. X#endif /* emacs */
  136. X
  137. X/* We write fatal error messages on standard error.  */
  138. X#include <stdio.h>
  139. X
  140. X/* isalpha(3) etc. are used for the character classes.  */
  141. X#include <ctype.h>
  142. X/* Sequents are missing isgraph.  */
  143. X#ifndef isgraph
  144. X#define isgraph(c) (isprint((c)) && !isspace((c)))
  145. X#endif
  146. X
  147. X/* Get the interface, including the syntax bits.  */
  148. X#include "regex.h"
  149. X
  150. X
  151. X/* These are the command codes that appear in compiled regular
  152. X   expressions, one per byte.  Some command codes are followed by
  153. X   argument bytes.  A command code can specify any interpretation
  154. X   whatsoever for its arguments.  Zero-bytes may appear in the compiled
  155. X   regular expression.
  156. X   
  157. X   The value of `exactn' is needed in search.c (search_buffer) in emacs.
  158. X   So regex.h defines a symbol `RE_EXACTN_VALUE' to be 1; the value of
  159. X   `exactn' we use here must also be 1.  */
  160. X
  161. Xenum regexpcode
  162. X  {
  163. X    unused=0,
  164. X    exactn=1, /* Followed by one byte giving n, then by n literal bytes.  */
  165. X    begline,  /* Fail unless at beginning of line.  */
  166. X    endline,  /* Fail unless at end of line.  */
  167. X    jump,     /* Followed by two bytes giving relative address to jump to.  */
  168. X    on_failure_jump,     /* Followed by two bytes giving relative address of 
  169. X                place to resume at in case of failure.  */
  170. X    finalize_jump,     /* Throw away latest failure point and then jump to 
  171. X                address.  */
  172. X    maybe_finalize_jump, /* Like jump but finalize if safe to do so.
  173. X                This is used to jump back to the beginning
  174. X                of a repeat.  If the command that follows
  175. X                this jump is clearly incompatible with the
  176. X                one at the beginning of the repeat, such that
  177. X                we can be sure that there is no use backtracking
  178. X                out of repetitions already completed,
  179. X                then we finalize.  */
  180. X    dummy_failure_jump,  /* Jump, and push a dummy failure point. This 
  181. X                failure point will be thrown away if an attempt 
  182. X                            is made to use it for a failure. A + construct 
  183. X                            makes this before the first repeat.  Also
  184. X                            use it as an intermediary kind of jump when
  185. X                            compiling an or construct.  */
  186. X    succeed_n,     /* Used like on_failure_jump except has to succeed n times;
  187. X            then gets turned into an on_failure_jump. The relative
  188. X                    address following it is useless until then.  The
  189. X                    address is followed by two bytes containing n.  */
  190. X    jump_n,     /* Similar to jump, but jump n times only; also the relative
  191. X            address following is in turn followed by yet two more bytes
  192. X                    containing n.  */
  193. X    set_number_at,    /* Set the following relative location to the
  194. X               subsequent number.  */
  195. X    anychar,     /* Matches any (more or less) one character.  */
  196. X    charset,     /* Matches any one char belonging to specified set.
  197. X            First following byte is number of bitmap bytes.
  198. X            Then come bytes for a bitmap saying which chars are in.
  199. X            Bits in each byte are ordered low-bit-first.
  200. X            A character is in the set if its bit is 1.
  201. X            A character too large to have a bit in the map
  202. X            is automatically not in the set.  */
  203. X    charset_not, /* Same parameters as charset, but match any character
  204. X                    that is not one of those specified.  */
  205. X    start_memory, /* Start remembering the text that is matched, for
  206. X            storing in a memory register.  Followed by one
  207. X                    byte containing the register number.  Register numbers
  208. X                    must be in the range 0 through RE_NREGS.  */
  209. X    stop_memory, /* Stop remembering the text that is matched
  210. X            and store it in a memory register.  Followed by
  211. X                    one byte containing the register number. Register
  212. X                    numbers must be in the range 0 through RE_NREGS.  */
  213. X    duplicate,   /* Match a duplicate of something remembered.
  214. X            Followed by one byte containing the index of the memory 
  215. X                    register.  */
  216. X    before_dot,     /* Succeeds if before point.  */
  217. X    at_dot,     /* Succeeds if at point.  */
  218. X    after_dot,     /* Succeeds if after point.  */
  219. X    begbuf,      /* Succeeds if at beginning of buffer.  */
  220. X    endbuf,      /* Succeeds if at end of buffer.  */
  221. X    wordchar,    /* Matches any word-constituent character.  */
  222. X    notwordchar, /* Matches any char that is not a word-constituent.  */
  223. X    wordbeg,     /* Succeeds if at word beginning.  */
  224. X    wordend,     /* Succeeds if at word end.  */
  225. X    wordbound,   /* Succeeds if at a word boundary.  */
  226. X    notwordbound,/* Succeeds if not at a word boundary.  */
  227. X    syntaxspec,  /* Matches any character whose syntax is specified.
  228. X            followed by a byte which contains a syntax code,
  229. X                    e.g., Sword.  */
  230. X    notsyntaxspec /* Matches any character whose syntax differs from
  231. X                     that specified.  */
  232. X  };
  233. X
  234. X/* Number of failure points to allocate space for initially,
  235. X   when matching.  If this number is exceeded, more space is allocated,
  236. X   so it is not a hard limit.  */
  237. X
  238. X#ifndef NFAILURES
  239. X#define NFAILURES 80
  240. X#endif
  241. X
  242. X#ifdef CHAR_UNSIGNED
  243. X#define SIGN_EXTEND_CHAR(c) ((c)>(char)127?(c)-256:(c)) /* for IBM RT */
  244. X#endif
  245. X#ifndef SIGN_EXTEND_CHAR
  246. X#define SIGN_EXTEND_CHAR(x) (x)
  247. X#endif
  248. X
  249. X/* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  250. X#define STORE_NUMBER(destination, number)                \
  251. X  { (destination)[0] = (number) & 0377;                    \
  252. X    (destination)[1] = (number) >> 8; }
  253. X  
  254. X/* Same as STORE_NUMBER, except increment the destination pointer to
  255. X   the byte after where the number is stored.  Watch out that values for
  256. X   DESTINATION such as p + 1 won't work, whereas p will.  */
  257. X#define STORE_NUMBER_AND_INCR(destination, number)            \
  258. X  { STORE_NUMBER(destination, number);                    \
  259. X    (destination) += 2; }
  260. X
  261. X
  262. X/* Put into DESTINATION a number stored in two contingous bytes starting
  263. X   at SOURCE.  */
  264. X#define EXTRACT_NUMBER(destination, source)                \
  265. X  { (destination) = *(source) & 0377;                    \
  266. X    (destination) += SIGN_EXTEND_CHAR (*(char *)((source) + 1)) << 8; }
  267. X
  268. X/* Same as EXTRACT_NUMBER, except increment the pointer for source to
  269. X   point to second byte of SOURCE.  Note that SOURCE has to be a value
  270. X   such as p, not, e.g., p + 1. */
  271. X#define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  272. X  { EXTRACT_NUMBER (destination, source);                \
  273. X    (source) += 2; }
  274. X
  275. X
  276. X/* Specify the precise syntax of regexps for compilation.  This provides
  277. X   for compatibility for various utilities which historically have
  278. X   different, incompatible syntaxes.
  279. X   
  280. X   The argument SYNTAX is a bit-mask comprised of the various bits
  281. X   defined in regex.h.  */
  282. X
  283. Xint
  284. Xre_set_syntax (syntax)
  285. X  int syntax;
  286. X{
  287. X  int ret;
  288. X
  289. X  ret = obscure_syntax;
  290. X  obscure_syntax = syntax;
  291. X  return ret;
  292. X}
  293. X
  294. X/* Set by re_set_syntax to the current regexp syntax to recognize.  */
  295. Xint obscure_syntax = 0;
  296. X
  297. X
  298. X
  299. X/* Macros for re_compile_pattern, which is found below these definitions.  */
  300. X
  301. X#define CHAR_CLASS_MAX_LENGTH  6
  302. X
  303. X/* Fetch the next character in the uncompiled pattern, translating it if
  304. X   necessary.  */
  305. X#define PATFETCH(c)                            \
  306. X  {if (p == pend) goto end_of_pattern;                    \
  307. X  c = * (unsigned char *) p++;                        \
  308. X  if (translate) c = translate[c]; }
  309. X
  310. X/* Fetch the next character in the uncompiled pattern, with no
  311. X   translation.  */
  312. X#define PATFETCH_RAW(c)                            \
  313. X {if (p == pend) goto end_of_pattern;                    \
  314. X  c = * (unsigned char *) p++; }
  315. X
  316. X#define PATUNFETCH p--
  317. X
  318. X
  319. X/* If the buffer isn't allocated when it comes in, use this.  */
  320. X#define INIT_BUF_SIZE  28
  321. X
  322. X/* Make sure we have at least N more bytes of space in buffer.  */
  323. X#define GET_BUFFER_SPACE(n)                        \
  324. X  {                                        \
  325. X    while (b - bufp->buffer + (n) >= bufp->allocated)            \
  326. X      EXTEND_BUFFER;                            \
  327. X  }
  328. X
  329. X/* Make sure we have one more byte of buffer space and then add CH to it.  */
  330. X#define BUFPUSH(ch)                            \
  331. X  {                                    \
  332. X    GET_BUFFER_SPACE (1);                        \
  333. X    *b++ = (char) (ch);                            \
  334. X  }
  335. X  
  336. X/* Extend the buffer by twice its current size via reallociation and
  337. X   reset the pointers that pointed into the old allocation to point to
  338. X   the correct places in the new allocation.  If extending the buffer
  339. X   results in it being larger than 1 << 16, then flag memory exhausted.  */
  340. X#define EXTEND_BUFFER                            \
  341. X  { char *old_buffer = bufp->buffer;                    \
  342. X    if (bufp->allocated == (1L<<16)) goto too_big;            \
  343. X    bufp->allocated *= 2;                        \
  344. X    if (bufp->allocated > (1L<<16)) bufp->allocated = (1L<<16);        \
  345. X    bufp->buffer = (char *) realloc (bufp->buffer, bufp->allocated);    \
  346. X    if (bufp->buffer == 0)                        \
  347. X      goto memory_exhausted;                        \
  348. X    b = (b - old_buffer) + bufp->buffer;                \
  349. X    if (fixup_jump)                            \
  350. X      fixup_jump = (fixup_jump - old_buffer) + bufp->buffer;        \
  351. X    if (laststart)                            \
  352. X      laststart = (laststart - old_buffer) + bufp->buffer;        \
  353. X    begalt = (begalt - old_buffer) + bufp->buffer;            \
  354. X    if (pending_exact)                            \
  355. X      pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  356. X  }
  357. X
  358. X/* Set the bit for character C in a character set list.  */
  359. X#define SET_LIST_BIT(c)  (b[(c) / BYTEWIDTH] |= 1 << ((c) % BYTEWIDTH))
  360. X
  361. X/* Get the next unsigned number in the uncompiled pattern.  */
  362. X#define GET_UNSIGNED_NUMBER(num)                     \
  363. X  { if (p != pend)                             \
  364. X      {                                 \
  365. X        PATFETCH (c);                             \
  366. X    while (isdigit (c))                         \
  367. X      {                                 \
  368. X        if (num < 0)                         \
  369. X           num = 0;                         \
  370. X            num = num * 10 + c - '0';                     \
  371. X        if (p == pend)                         \
  372. X           break;                             \
  373. X        PATFETCH (c);                         \
  374. X      }                                 \
  375. X        }                                 \
  376. X  }
  377. X
  378. X/* Subroutines for re_compile_pattern.  */
  379. Xstatic void store_jump (), insert_jump (), store_jump_n (),
  380. X        insert_jump_n (), insert_op_2 ();
  381. X
  382. X
  383. X/* re_compile_pattern takes a regular-expression string
  384. X   and converts it into a buffer full of byte commands for matching.
  385. X
  386. X   PATTERN   is the address of the pattern string
  387. X   SIZE      is the length of it.
  388. X   BUFP        is a  struct re_pattern_buffer *  which points to the info
  389. X         on where to store the byte commands.
  390. X         This structure contains a  char *  which points to the
  391. X         actual space, which should have been obtained with malloc.
  392. X         re_compile_pattern may use realloc to grow the buffer space.
  393. X
  394. X   The number of bytes of commands can be found out by looking in
  395. X   the `struct re_pattern_buffer' that bufp pointed to, after
  396. X   re_compile_pattern returns. */
  397. X
  398. Xchar *
  399. Xre_compile_pattern (pattern, size, bufp)
  400. X     char *pattern;
  401. X     int size;
  402. X     struct re_pattern_buffer *bufp;
  403. X{
  404. X  register char *b = bufp->buffer;
  405. X  register char *p = pattern;
  406. X  char *pend = pattern + size;
  407. X  register unsigned c, c1;
  408. X  char *p1;
  409. X  unsigned char *translate = (unsigned char *) bufp->translate;
  410. X
  411. X  /* Address of the count-byte of the most recently inserted `exactn'
  412. X     command.  This makes it possible to tell whether a new exact-match
  413. X     character can be added to that command or requires a new `exactn'
  414. X     command.  */
  415. X     
  416. X  char *pending_exact = 0;
  417. X
  418. X  /* Address of the place where a forward-jump should go to the end of
  419. X     the containing expression.  Each alternative of an `or', except the
  420. X     last, ends with a forward-jump of this sort.  */
  421. X
  422. X  char *fixup_jump = 0;
  423. X
  424. X  /* Address of start of the most recently finished expression.
  425. X     This tells postfix * where to find the start of its operand.  */
  426. X
  427. X  char *laststart = 0;
  428. X
  429. X  /* In processing a repeat, 1 means zero matches is allowed.  */
  430. X
  431. X  char zero_times_ok;
  432. X
  433. X  /* In processing a repeat, 1 means many matches is allowed.  */
  434. X
  435. X  char many_times_ok;
  436. X
  437. X  /* Address of beginning of regexp, or inside of last \(.  */
  438. X
  439. X  char *begalt = b;
  440. X
  441. X  /* In processing an interval, at least this many matches must be made.  */
  442. X  int lower_bound;
  443. X
  444. X  /* In processing an interval, at most this many matches can be made.  */
  445. X  int upper_bound;
  446. X
  447. X  /* Place in pattern (i.e., the {) to which to go back if the interval
  448. X     is invalid.  */
  449. X  char *beg_interval = 0;
  450. X  
  451. X  /* Stack of information saved by \( and restored by \).
  452. X     Four stack elements are pushed by each \(:
  453. X       First, the value of b.
  454. X       Second, the value of fixup_jump.
  455. X       Third, the value of regnum.
  456. X       Fourth, the value of begalt.  */
  457. X
  458. X  int stackb[40];
  459. X  int *stackp = stackb;
  460. X  int *stacke = stackb + 40;
  461. X  int *stackt;
  462. X
  463. X  /* Counts \('s as they are encountered.  Remembered for the matching \),
  464. X     where it becomes the register number to put in the stop_memory
  465. X     command.  */
  466. X
  467. X  int regnum = 1;
  468. X
  469. X  bufp->fastmap_accurate = 0;
  470. X
  471. X#ifndef emacs
  472. X#ifndef SYNTAX_TABLE
  473. X  /* Initialize the syntax table.  */
  474. X   init_syntax_once();
  475. X#endif
  476. X#endif
  477. X
  478. X  if (bufp->allocated == 0)
  479. X    {
  480. X      bufp->allocated = INIT_BUF_SIZE;
  481. X      if (bufp->buffer)
  482. X    /* EXTEND_BUFFER loses when bufp->allocated is 0.  */
  483. X    bufp->buffer = (char *) realloc (bufp->buffer, INIT_BUF_SIZE);
  484. X      else
  485. X    /* Caller did not allocate a buffer.  Do it for them.  */
  486. X    bufp->buffer = (char *) malloc (INIT_BUF_SIZE);
  487. X      if (!bufp->buffer) goto memory_exhausted;
  488. X      begalt = b = bufp->buffer;
  489. X    }
  490. X
  491. X  while (p != pend)
  492. X    {
  493. X      PATFETCH (c);
  494. X
  495. X      switch (c)
  496. X    {
  497. X    case '$':
  498. X      {
  499. X        char *p1 = p;
  500. X        /* When testing what follows the $,
  501. X           look past the \-constructs that don't consume anything.  */
  502. X        if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  503. X          while (p1 != pend)
  504. X        {
  505. X          if (*p1 == '\\' && p1 + 1 != pend
  506. X              && (p1[1] == '<' || p1[1] == '>'
  507. X              || p1[1] == '`' || p1[1] == '\''
  508. X#ifdef emacs
  509. X              || p1[1] == '='
  510. X#endif
  511. X              || p1[1] == 'b' || p1[1] == 'B'))
  512. X            p1 += 2;
  513. X          else
  514. X            break;
  515. X        }
  516. X            if (obscure_syntax & RE_TIGHT_VBAR)
  517. X          {
  518. X        if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS) && p1 != pend)
  519. X          goto normal_char;
  520. X        /* Make operand of last vbar end before this `$'.  */
  521. X        if (fixup_jump)
  522. X          store_jump (fixup_jump, jump, b);
  523. X        fixup_jump = 0;
  524. X        BUFPUSH (endline);
  525. X        break;
  526. X          }
  527. X        /* $ means succeed if at end of line, but only in special contexts.
  528. X          If validly in the middle of a pattern, it is a normal character. */
  529. X
  530. X            if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && p1 != pend)
  531. X          goto invalid_pattern;
  532. X        if (p1 == pend || *p1 == '\n'
  533. X        || (obscure_syntax & RE_CONTEXT_INDEP_OPS)
  534. X        || (obscure_syntax & RE_NO_BK_PARENS
  535. X            ? *p1 == ')'
  536. X            : *p1 == '\\' && p1[1] == ')')
  537. X        || (obscure_syntax & RE_NO_BK_VBAR
  538. X            ? *p1 == '|'
  539. X            : *p1 == '\\' && p1[1] == '|'))
  540. X          {
  541. X        BUFPUSH (endline);
  542. X        break;
  543. X          }
  544. X        goto normal_char;
  545. X          }
  546. X    case '^':
  547. X      /* ^ means succeed if at beg of line, but only if no preceding 
  548. X             pattern.  */
  549. X             
  550. X          if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS) && laststart)
  551. X            goto invalid_pattern;
  552. X          if (laststart && p - 2 >= pattern && p[-2] != '\n'
  553. X           && !(obscure_syntax & RE_CONTEXT_INDEP_OPS))
  554. X        goto normal_char;
  555. X      if (obscure_syntax & RE_TIGHT_VBAR)
  556. X        {
  557. X          if (p != pattern + 1
  558. X          && ! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  559. X        goto normal_char;
  560. X          BUFPUSH (begline);
  561. X          begalt = b;
  562. X        }
  563. X      else
  564. X        BUFPUSH (begline);
  565. X      break;
  566. X
  567. X    case '+':
  568. X    case '?':
  569. X      if ((obscure_syntax & RE_BK_PLUS_QM)
  570. X          || (obscure_syntax & RE_LIMITED_OPS))
  571. X        goto normal_char;
  572. X    handle_plus:
  573. X    case '*':
  574. X      /* If there is no previous pattern, char not special. */
  575. X      if (!laststart)
  576. X            {
  577. X              if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  578. X                goto invalid_pattern;
  579. X              else if (! (obscure_syntax & RE_CONTEXT_INDEP_OPS))
  580. X        goto normal_char;
  581. X            }
  582. X      /* If there is a sequence of repetition chars,
  583. X         collapse it down to just one.  */
  584. X      zero_times_ok = 0;
  585. X      many_times_ok = 0;
  586. X      while (1)
  587. X        {
  588. X          zero_times_ok |= c != '+';
  589. X          many_times_ok |= c != '?';
  590. X          if (p == pend)
  591. X        break;
  592. X          PATFETCH (c);
  593. X          if (c == '*')
  594. X        ;
  595. X          else if (!(obscure_syntax & RE_BK_PLUS_QM)
  596. X               && (c == '+' || c == '?'))
  597. X        ;
  598. X          else if ((obscure_syntax & RE_BK_PLUS_QM)
  599. X               && c == '\\')
  600. X        {
  601. X          int c1;
  602. X          PATFETCH (c1);
  603. X          if (!(c1 == '+' || c1 == '?'))
  604. X            {
  605. X              PATUNFETCH;
  606. X              PATUNFETCH;
  607. X              break;
  608. X            }
  609. X          c = c1;
  610. X        }
  611. X          else
  612. X        {
  613. X          PATUNFETCH;
  614. X          break;
  615. X        }
  616. X        }
  617. X
  618. X      /* Star, etc. applied to an empty pattern is equivalent
  619. X         to an empty pattern.  */
  620. X      if (!laststart)  
  621. X        break;
  622. X
  623. X      /* Now we know whether or not zero matches is allowed
  624. X         and also whether or not two or more matches is allowed.  */
  625. X      if (many_times_ok)
  626. X        {
  627. X          /* If more than one repetition is allowed, put in at the
  628. X                 end a backward relative jump from b to before the next
  629. X                 jump we're going to put in below (which jumps from
  630. X                 laststart to after this jump).  */
  631. X              GET_BUFFER_SPACE (3);
  632. X          store_jump (b, maybe_finalize_jump, laststart - 3);
  633. X          b += 3;      /* Because store_jump put stuff here.  */
  634. X        }
  635. X          /* On failure, jump from laststart to b + 3, which will be the
  636. X             end of the buffer after this jump is inserted.  */
  637. X          GET_BUFFER_SPACE (3);
  638. X      insert_jump (on_failure_jump, laststart, b + 3, b);
  639. X      pending_exact = 0;
  640. X      b += 3;
  641. X      if (!zero_times_ok)
  642. X        {
  643. X          /* At least one repetition is required, so insert a
  644. X                 dummy-failure before the initial on-failure-jump
  645. X                 instruction of the loop. This effects a skip over that
  646. X                 instruction the first time we hit that loop.  */
  647. X              GET_BUFFER_SPACE (6);
  648. X              insert_jump (dummy_failure_jump, laststart, laststart + 6, b);
  649. X          b += 3;
  650. X        }
  651. X      break;
  652. X
  653. X    case '.':
  654. X      laststart = b;
  655. X      BUFPUSH (anychar);
  656. X      break;
  657. X
  658. X        case '[':
  659. X          if (p == pend)
  660. X            goto invalid_pattern;
  661. X      while (b - bufp->buffer
  662. X         > bufp->allocated - 3 - (1 << BYTEWIDTH) / BYTEWIDTH)
  663. X        EXTEND_BUFFER;
  664. X
  665. X      laststart = b;
  666. X      if (*p == '^')
  667. X        {
  668. X              BUFPUSH (charset_not); 
  669. X              p++;
  670. X            }
  671. X      else
  672. X        BUFPUSH (charset);
  673. X      p1 = p;
  674. X
  675. X      BUFPUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  676. X      /* Clear the whole map */
  677. X      bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  678. X          
  679. X      if ((obscure_syntax & RE_HAT_NOT_NEWLINE) && b[-2] == charset_not)
  680. X            SET_LIST_BIT ('\n');
  681. X
  682. X
  683. X      /* Read in characters and ranges, setting map bits.  */
  684. X      while (1)
  685. X        {
  686. X          PATFETCH (c);
  687. X
  688. X          /* If set, \ escapes characters when inside [...].  */
  689. X          if ((obscure_syntax & RE_AWK_CLASS_HACK) && c == '\\')
  690. X            {
  691. X              PATFETCH(c1);
  692. X                  SET_LIST_BIT (c1);
  693. X              continue;
  694. X            }
  695. X              if (c == ']')
  696. X                {
  697. X                  if (p == p1 + 1)
  698. X                    {
  699. X              /* If this is an empty bracket expression.  */
  700. X                      if ((obscure_syntax & RE_NO_EMPTY_BRACKETS) 
  701. X                          && p == pend)
  702. X                        goto invalid_pattern;
  703. X                    }
  704. X                  else 
  705. X            /* Stop if this isn't merely a ] inside a bracket
  706. X                       expression, but rather the end of a bracket
  707. X                       expression.  */
  708. X                    break;
  709. X                }
  710. X              /* Get a range.  */
  711. X              if (p[0] == '-' && p[1] != ']')
  712. X        {
  713. X                  PATFETCH (c1);
  714. X          PATFETCH (c1);
  715. X                  
  716. X          if ((obscure_syntax & RE_NO_EMPTY_RANGES) && c > c1)
  717. X                    goto invalid_pattern;
  718. X                    
  719. X          if ((obscure_syntax & RE_NO_HYPHEN_RANGE_END) 
  720. X                      && c1 == '-' && *p != ']')
  721. X                    goto invalid_pattern;
  722. X                    
  723. X                  while (c <= c1)
  724. X            {
  725. X                      SET_LIST_BIT (c);
  726. X                      c++;
  727. X            }
  728. X                }
  729. X          else if ((obscure_syntax & RE_CHAR_CLASSES)
  730. X            &&  c == '[' && p[0] == ':')
  731. X                {
  732. X          /* Longest valid character class word has six characters.  */
  733. X                  char str[CHAR_CLASS_MAX_LENGTH];
  734. X          PATFETCH (c);
  735. X          c1 = 0;
  736. X          /* If no ] at end.  */
  737. X                  if (p == pend)
  738. X                    goto invalid_pattern;
  739. X          while (1)
  740. X            {
  741. X              /* Don't translate the ``character class'' characters.  */
  742. X                      PATFETCH_RAW (c);
  743. X              if (c == ':' || c == ']' || p == pend
  744. X                          || c1 == CHAR_CLASS_MAX_LENGTH)
  745. X                break;
  746. X              str[c1++] = c;
  747. X            }
  748. X          str[c1] = '\0';
  749. X          if (p == pend     
  750. X              || c == ']'    /* End of the bracket expression.  */
  751. X                      || p[0] != ']'
  752. X              || p + 1 == pend
  753. X                      || (strcmp (str, "alpha") != 0 
  754. X                          && strcmp (str, "upper") != 0
  755. X              && strcmp (str, "lower") != 0 
  756. X                          && strcmp (str, "digit") != 0
  757. X              && strcmp (str, "alnum") != 0 
  758. X                          && strcmp (str, "xdigit") != 0
  759. X              && strcmp (str, "space") != 0 
  760. X                          && strcmp (str, "print") != 0
  761. X              && strcmp (str, "punct") != 0 
  762. X                          && strcmp (str, "graph") != 0
  763. X              && strcmp (str, "cntrl") != 0))
  764. X            {
  765. X               /* Undo the ending character, the letters, and leave 
  766. X                          the leading : and [ (but set bits for them).  */
  767. X                      c1++;
  768. X              while (c1--)    
  769. X            PATUNFETCH;
  770. X              SET_LIST_BIT ('[');
  771. X              SET_LIST_BIT (':');
  772. X                }
  773. X                  else
  774. X                    {
  775. X                      /* The ] at the end of the character class.  */
  776. X                      PATFETCH (c);                    
  777. X                      if (c != ']')
  778. X                        goto invalid_pattern;
  779. X              for (c = 0; c < (1 << BYTEWIDTH); c++)
  780. X            {
  781. X              if ((strcmp (str, "alpha") == 0  && isalpha (c))
  782. X                   || (strcmp (str, "upper") == 0  && isupper (c))
  783. X                   || (strcmp (str, "lower") == 0  && islower (c))
  784. X                   || (strcmp (str, "digit") == 0  && isdigit (c))
  785. X                   || (strcmp (str, "alnum") == 0  && isalnum (c))
  786. X                   || (strcmp (str, "xdigit") == 0  && isxdigit (c))
  787. X                   || (strcmp (str, "space") == 0  && isspace (c))
  788. X                   || (strcmp (str, "print") == 0  && isprint (c))
  789. X                   || (strcmp (str, "punct") == 0  && ispunct (c))
  790. X                   || (strcmp (str, "graph") == 0  && isgraph (c))
  791. X                   || (strcmp (str, "cntrl") == 0  && iscntrl (c)))
  792. X                SET_LIST_BIT (c);
  793. X            }
  794. X            }
  795. X                }
  796. X              else
  797. X                SET_LIST_BIT (c);
  798. X        }
  799. X
  800. X          /* Discard any character set/class bitmap bytes that are all
  801. X             0 at the end of the map. Decrement the map-length byte too.  */
  802. X          while ((int) b[-1] > 0 && b[b[-1] - 1] == 0) 
  803. X            b[-1]--; 
  804. X          b += b[-1];
  805. X          break;
  806. X
  807. X    case '(':
  808. X      if (! (obscure_syntax & RE_NO_BK_PARENS))
  809. X        goto normal_char;
  810. X      else
  811. X        goto handle_open;
  812. X
  813. X    case ')':
  814. X      if (! (obscure_syntax & RE_NO_BK_PARENS))
  815. X        goto normal_char;
  816. X      else
  817. X        goto handle_close;
  818. X
  819. X        case '\n':
  820. X      if (! (obscure_syntax & RE_NEWLINE_OR))
  821. X        goto normal_char;
  822. X      else
  823. X        goto handle_bar;
  824. X
  825. X    case '|':
  826. X      if ((obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  827. X              && (! laststart  ||  p == pend))
  828. X        goto invalid_pattern;
  829. X          else if (! (obscure_syntax & RE_NO_BK_VBAR))
  830. X        goto normal_char;
  831. X      else
  832. X        goto handle_bar;
  833. X
  834. X    case '{':
  835. X           if (! ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  836. X                  && (obscure_syntax & RE_INTERVALS)))
  837. X             goto normal_char;
  838. X           else
  839. X             goto handle_interval;
  840. X             
  841. X        case '\\':
  842. X      if (p == pend) goto invalid_pattern;
  843. X      PATFETCH_RAW (c);
  844. X      switch (c)
  845. X        {
  846. X        case '(':
  847. X          if (obscure_syntax & RE_NO_BK_PARENS)
  848. X        goto normal_backsl;
  849. X        handle_open:
  850. X          if (stackp == stacke) goto nesting_too_deep;
  851. X
  852. X              /* Laststart should point to the start_memory that we are about
  853. X                 to push (unless the pattern has RE_NREGS or more ('s).  */
  854. X              *stackp++ = b - bufp->buffer;    
  855. X          if (regnum < RE_NREGS)
  856. X            {
  857. X          BUFPUSH (start_memory);
  858. X          BUFPUSH (regnum);
  859. X            }
  860. X          *stackp++ = fixup_jump ? fixup_jump - bufp->buffer + 1 : 0;
  861. X          *stackp++ = regnum++;
  862. X          *stackp++ = begalt - bufp->buffer;
  863. X          fixup_jump = 0;
  864. X          laststart = 0;
  865. X          begalt = b;
  866. X          break;
  867. X
  868. X        case ')':
  869. X          if (obscure_syntax & RE_NO_BK_PARENS)
  870. X        goto normal_backsl;
  871. X        handle_close:
  872. X          if (stackp == stackb) goto unmatched_close;
  873. X          begalt = *--stackp + bufp->buffer;
  874. X          if (fixup_jump)
  875. X        store_jump (fixup_jump, jump, b);
  876. X          if (stackp[-1] < RE_NREGS)
  877. X        {
  878. X          BUFPUSH (stop_memory);
  879. X          BUFPUSH (stackp[-1]);
  880. X        }
  881. X          stackp -= 2;
  882. X              fixup_jump = *stackp ? *stackp + bufp->buffer - 1 : 0;
  883. X              laststart = *--stackp + bufp->buffer;
  884. X          break;
  885. X
  886. X        case '|':
  887. X              if ((obscure_syntax & RE_LIMITED_OPS)
  888. X              || (obscure_syntax & RE_NO_BK_VBAR))
  889. X        goto normal_backsl;
  890. X        handle_bar:
  891. X              if (obscure_syntax & RE_LIMITED_OPS)
  892. X                goto normal_char;
  893. X          /* Insert before the previous alternative a jump which
  894. X                 jumps to this alternative if the former fails.  */
  895. X              GET_BUFFER_SPACE (6);
  896. X              insert_jump (on_failure_jump, begalt, b + 6, b);
  897. X          pending_exact = 0;
  898. X          b += 3;
  899. X          /* The alternative before the previous alternative has a
  900. X                 jump after it which gets executed if it gets matched.
  901. X                 Adjust that jump so it will jump to the previous
  902. X                 alternative's analogous jump (put in below, which in
  903. X                 turn will jump to the next (if any) alternative's such
  904. X                 jump, etc.).  The last such jump jumps to the correct
  905. X                 final destination.  */
  906. X              if (fixup_jump)
  907. X        store_jump (fixup_jump, jump, b);
  908. X                
  909. X          /* Leave space for a jump after previous alternative---to be 
  910. X                 filled in later.  */
  911. X              fixup_jump = b;
  912. X              b += 3;
  913. X
  914. X              laststart = 0;
  915. X          begalt = b;
  916. X          break;
  917. X
  918. X            case '{': 
  919. X              if (! (obscure_syntax & RE_INTERVALS)
  920. X          /* Let \{ be a literal.  */
  921. X                  || ((obscure_syntax & RE_INTERVALS)
  922. X                      && (obscure_syntax & RE_NO_BK_CURLY_BRACES))
  923. X          /* If it's the string "\{".  */
  924. X          || (p - 2 == pattern  &&  p == pend))
  925. X                goto normal_backsl;
  926. X            handle_interval:
  927. X          beg_interval = p - 1;        /* The {.  */
  928. X              /* If there is no previous pattern, this isn't an interval.  */
  929. X          if (!laststart)
  930. X            {
  931. X                  if (obscure_syntax & RE_CONTEXTUAL_INVALID_OPS)
  932. X            goto invalid_pattern;
  933. X                  else
  934. X                    goto normal_backsl;
  935. X                }
  936. X              /* It also isn't an interval if not preceded by an re
  937. X                 matching a single character or subexpression, or if
  938. X                 the current type of intervals can't handle back
  939. X                 references and the previous thing is a back reference.  */
  940. X              if (! (*laststart == anychar
  941. X             || *laststart == charset
  942. X             || *laststart == charset_not
  943. X             || *laststart == start_memory
  944. X             || (*laststart == exactn  &&  laststart[1] == 1)
  945. X             || (! (obscure_syntax & RE_NO_BK_REFS)
  946. X                         && *laststart == duplicate)))
  947. X                {
  948. X                  if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  949. X                    goto normal_char;
  950. X                    
  951. X          /* Posix extended syntax is handled in previous
  952. X                     statement; this is for Posix basic syntax.  */
  953. X                  if (obscure_syntax & RE_INTERVALS)
  954. X                    goto invalid_pattern;
  955. X                    
  956. X                  goto normal_backsl;
  957. X        }
  958. X              lower_bound = -1;            /* So can see if are set.  */
  959. X          upper_bound = -1;
  960. X              GET_UNSIGNED_NUMBER (lower_bound);
  961. X          if (c == ',')
  962. X        {
  963. X          GET_UNSIGNED_NUMBER (upper_bound);
  964. X          if (upper_bound < 0)
  965. X            upper_bound = RE_DUP_MAX;
  966. X        }
  967. X          if (upper_bound < 0)
  968. X        upper_bound = lower_bound;
  969. X              if (! (obscure_syntax & RE_NO_BK_CURLY_BRACES)) 
  970. X                {
  971. X                  if (c != '\\')
  972. X                    goto invalid_pattern;
  973. X                  PATFETCH (c);
  974. X                }
  975. X          if (c != '}' || lower_bound < 0 || upper_bound > RE_DUP_MAX
  976. X          || lower_bound > upper_bound 
  977. X                  || ((obscure_syntax & RE_NO_BK_CURLY_BRACES) 
  978. X              && p != pend  && *p == '{')) 
  979. X            {
  980. X          if (obscure_syntax & RE_NO_BK_CURLY_BRACES)
  981. X                    goto unfetch_interval;
  982. X                  else
  983. X                    goto invalid_pattern;
  984. X        }
  985. X
  986. X          /* If upper_bound is zero, don't want to succeed at all; 
  987. X          jump from laststart to b + 3, which will be the end of
  988. X                 the buffer after this jump is inserted.  */
  989. X                 
  990. X               if (upper_bound == 0)
  991. X                 {
  992. X                   GET_BUFFER_SPACE (3);
  993. X                   insert_jump (jump, laststart, b + 3, b);
  994. X                   b += 3;
  995. X                 }
  996. X
  997. X               /* Otherwise, after lower_bound number of succeeds, jump
  998. X                  to after the jump_n which will be inserted at the end
  999. X                  of the buffer, and insert that jump_n.  */
  1000. X               else 
  1001. X         { /* Set to 5 if only one repetition is allowed and
  1002. X                  hence no jump_n is inserted at the current end of
  1003. X                      the buffer; then only space for the succeed_n is
  1004. X                      needed.  Otherwise, need space for both the
  1005. X                      succeed_n and the jump_n.  */
  1006. X                      
  1007. X                   unsigned slots_needed = upper_bound == 1 ? 5 : 10;
  1008. X                     
  1009. X                   GET_BUFFER_SPACE (slots_needed);
  1010. X                   /* Initialize the succeed_n to n, even though it will
  1011. X                      be set by its attendant set_number_at, because
  1012. X                      re_compile_fastmap will need to know it.  Jump to
  1013. X                      what the end of buffer will be after inserting
  1014. X                      this succeed_n and possibly appending a jump_n.  */
  1015. X                   insert_jump_n (succeed_n, laststart, b + slots_needed, 
  1016. X                          b, lower_bound);
  1017. X                   b += 5;     /* Just increment for the succeed_n here.  */
  1018. X
  1019. X          /* More than one repetition is allowed, so put in at
  1020. X             the end of the buffer a backward jump from b to the
  1021. X                     succeed_n we put in above.  By the time we've gotten
  1022. X                     to this jump when matching, we'll have matched once
  1023. X                     already, so jump back only upper_bound - 1 times.  */
  1024. X
  1025. X                   if (upper_bound > 1)
  1026. X                     {
  1027. X                       store_jump_n (b, jump_n, laststart, upper_bound - 1);
  1028. X                       b += 5;
  1029. X                       /* When hit this when matching, reset the
  1030. X                          preceding jump_n's n to upper_bound - 1.  */
  1031. X                       BUFPUSH (set_number_at);
  1032. X               GET_BUFFER_SPACE (2);
  1033. X                       STORE_NUMBER_AND_INCR (b, -5);
  1034. X                       STORE_NUMBER_AND_INCR (b, upper_bound - 1);
  1035. X                     }
  1036. X           /* When hit this when matching, set the succeed_n's n.  */
  1037. X                   GET_BUFFER_SPACE (5);
  1038. X           insert_op_2 (set_number_at, laststart, b, 5, lower_bound);
  1039. X                   b += 5;
  1040. X                 }
  1041. X              pending_exact = 0;
  1042. X          beg_interval = 0;
  1043. X              break;
  1044. X
  1045. X
  1046. X            unfetch_interval:
  1047. X          /* If an invalid interval, match the characters as literals.  */
  1048. X           if (beg_interval)
  1049. X                 p = beg_interval;
  1050. X             else
  1051. X                 {
  1052. X                   fprintf (stderr, 
  1053. X              "regex: no interval beginning to which to backtrack.\n");
  1054. X           exit (1);
  1055. X                 }
  1056. X                 
  1057. X               beg_interval = 0;
  1058. X               PATFETCH (c);        /* normal_char expects char in `c'.  */
  1059. X           goto normal_char;
  1060. X           break;
  1061. X
  1062. X#ifdef emacs
  1063. X        case '=':
  1064. X          BUFPUSH (at_dot);
  1065. X          break;
  1066. X
  1067. X        case 's':    
  1068. X          laststart = b;
  1069. X          BUFPUSH (syntaxspec);
  1070. X          PATFETCH (c);
  1071. X          BUFPUSH (syntax_spec_code[c]);
  1072. X          break;
  1073. X
  1074. X        case 'S':
  1075. X          laststart = b;
  1076. X          BUFPUSH (notsyntaxspec);
  1077. X          PATFETCH (c);
  1078. X          BUFPUSH (syntax_spec_code[c]);
  1079. X          break;
  1080. X#endif /* emacs */
  1081. X
  1082. X        case 'w':
  1083. X          laststart = b;
  1084. X          BUFPUSH (wordchar);
  1085. X          break;
  1086. X
  1087. X        case 'W':
  1088. X          laststart = b;
  1089. X          BUFPUSH (notwordchar);
  1090. X          break;
  1091. X
  1092. X        case '<':
  1093. X          BUFPUSH (wordbeg);
  1094. X          break;
  1095. X
  1096. X        case '>':
  1097. X          BUFPUSH (wordend);
  1098. X          break;
  1099. X
  1100. X        case 'b':
  1101. X          BUFPUSH (wordbound);
  1102. X          break;
  1103. X
  1104. X        case 'B':
  1105. X          BUFPUSH (notwordbound);
  1106. X          break;
  1107. X
  1108. X        case '`':
  1109. X          BUFPUSH (begbuf);
  1110. X          break;
  1111. X
  1112. X        case '\'':
  1113. X          BUFPUSH (endbuf);
  1114. X          break;
  1115. X
  1116. X        case '1':
  1117. X        case '2':
  1118. X        case '3':
  1119. X        case '4':
  1120. X        case '5':
  1121. X        case '6':
  1122. X        case '7':
  1123. X        case '8':
  1124. X        case '9':
  1125. X          if (obscure_syntax & RE_NO_BK_REFS)
  1126. X                goto normal_char;
  1127. X              c1 = c - '0';
  1128. X          if (c1 >= regnum)
  1129. X        {
  1130. X            if (obscure_syntax & RE_NO_EMPTY_BK_REF)
  1131. X                    goto invalid_pattern;
  1132. X                  else
  1133. X                    goto normal_char;
  1134. X                }
  1135. X              /* Can't back reference to a subexpression if inside of it.  */
  1136. X              for (stackt = stackp - 2;  stackt > stackb;  stackt -= 4)
  1137. X         if (*stackt == c1)
  1138. X          goto normal_char;
  1139. X          laststart = b;
  1140. X          BUFPUSH (duplicate);
  1141. X          BUFPUSH (c1);
  1142. X          break;
  1143. X
  1144. X        case '+':
  1145. X        case '?':
  1146. X          if (obscure_syntax & RE_BK_PLUS_QM)
  1147. X        goto handle_plus;
  1148. X          else
  1149. X                goto normal_backsl;
  1150. X              break;
  1151. X
  1152. X            default:
  1153. X        normal_backsl:
  1154. X          /* You might think it would be useful for \ to mean
  1155. X         not to translate; but if we don't translate it
  1156. X         it will never match anything.  */
  1157. X          if (translate) c = translate[c];
  1158. X          goto normal_char;
  1159. X        }
  1160. X      break;
  1161. X
  1162. X    default:
  1163. X    normal_char:        /* Expects the character in `c'.  */
  1164. X      if (!pending_exact || pending_exact + *pending_exact + 1 != b
  1165. X          || *pending_exact == 0177 || *p == '*' || *p == '^'
  1166. X          || ((obscure_syntax & RE_BK_PLUS_QM)
  1167. X          ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  1168. X          : (*p == '+' || *p == '?'))
  1169. X          || ((obscure_syntax & RE_INTERVALS) 
  1170. X                  && ((obscure_syntax & RE_NO_BK_CURLY_BRACES)
  1171. X              ? *p == '{'
  1172. X                      : (p[0] == '\\' && p[1] == '{'))))
  1173. X        {
  1174. X          laststart = b;
  1175. X          BUFPUSH (exactn);
  1176. X          pending_exact = b;
  1177. X          BUFPUSH (0);
  1178. X        }
  1179. X      BUFPUSH (c);
  1180. X      (*pending_exact)++;
  1181. X    }
  1182. X    }
  1183. X
  1184. X  if (fixup_jump)
  1185. X    store_jump (fixup_jump, jump, b);
  1186. X
  1187. X  if (stackp != stackb) goto unmatched_open;
  1188. X
  1189. X  bufp->used = b - bufp->buffer;
  1190. X  return 0;
  1191. X
  1192. X invalid_pattern:
  1193. X  return "Invalid regular expression";
  1194. X
  1195. X unmatched_open:
  1196. X  return "Unmatched \\(";
  1197. X
  1198. X unmatched_close:
  1199. X  return "Unmatched \\)";
  1200. X
  1201. X end_of_pattern:
  1202. X  return "Premature end of regular expression";
  1203. X
  1204. X nesting_too_deep:
  1205. X  return "Nesting too deep";
  1206. X
  1207. X too_big:
  1208. X  return "Regular expression too big";
  1209. X
  1210. X memory_exhausted:
  1211. X  return "Memory exhausted";
  1212. X}
  1213. X
  1214. X
  1215. X/* Store a jump of the form <OPCODE> <relative address>.
  1216. X   Store in the location FROM a jump operation to jump to relative
  1217. X   address FROM - TO.  OPCODE is the opcode to store.  */
  1218. X
  1219. Xstatic void
  1220. Xstore_jump (from, opcode, to)
  1221. X     char *from, *to;
  1222. X     char opcode;
  1223. X{
  1224. X  from[0] = opcode;
  1225. X  STORE_NUMBER(from + 1, to - (from + 3));
  1226. X}
  1227. X
  1228. X
  1229. X/* Open up space before char FROM, and insert there a jump to TO.
  1230. X   CURRENT_END gives the end of the storage not in use, so we know 
  1231. X   how much data to copy up. OP is the opcode of the jump to insert.
  1232. X
  1233. X   If you call this function, you must zero out pending_exact.  */
  1234. X
  1235. Xstatic void
  1236. Xinsert_jump (op, from, to, current_end)
  1237. X     char op;
  1238. X     char *from, *to, *current_end;
  1239. X{
  1240. X  register char *pfrom = current_end;        /* Copy from here...  */
  1241. X  register char *pto = current_end + 3;        /* ...to here.  */
  1242. X
  1243. X  while (pfrom != from)                   
  1244. X    *--pto = *--pfrom;
  1245. X  store_jump (from, op, to);
  1246. X}
  1247. X
  1248. X
  1249. X/* Store a jump of the form <opcode> <relative address> <n> .
  1250. X
  1251. X   Store in the location FROM a jump operation to jump to relative
  1252. X   address FROM - TO.  OPCODE is the opcode to store, N is a number the
  1253. X   jump uses, say, to decide how many times to jump.
  1254. X   
  1255. X   If you call this function, you must zero out pending_exact.  */
  1256. X
  1257. Xstatic void
  1258. Xstore_jump_n (from, opcode, to, n)
  1259. X     char *from, *to;
  1260. X     char opcode;
  1261. X     unsigned n;
  1262. X{
  1263. X  from[0] = opcode;
  1264. X  STORE_NUMBER (from + 1, to - (from + 3));
  1265. X  STORE_NUMBER (from + 3, n);
  1266. X}
  1267. X
  1268. X
  1269. X/* Similar to insert_jump, but handles a jump which needs an extra
  1270. X   number to handle minimum and maximum cases.  Open up space at
  1271. X   location FROM, and insert there a jump to TO.  CURRENT_END gives the
  1272. X   end of the storage in use, so we know how much data to copy up. OP is
  1273. X   the opcode of the jump to insert.
  1274. X
  1275. X   If you call this function, you must zero out pending_exact.  */
  1276. X
  1277. Xstatic void
  1278. Xinsert_jump_n (op, from, to, current_end, n)
  1279. X     char op;
  1280. X     char *from, *to, *current_end;
  1281. X     unsigned n;
  1282. X{
  1283. X  register char *pfrom = current_end;        /* Copy from here...  */
  1284. X  register char *pto = current_end + 5;        /* ...to here.  */
  1285. X
  1286. X  while (pfrom != from)                   
  1287. X    *--pto = *--pfrom;
  1288. X  store_jump_n (from, op, to, n);
  1289. X}
  1290. X
  1291. X
  1292. X/* Open up space at location THERE, and insert operation OP followed by
  1293. X   NUM_1 and NUM_2.  CURRENT_END gives the end of the storage in use, so
  1294. X   we know how much data to copy up.
  1295. X
  1296. X   If you call this function, you must zero out pending_exact.  */
  1297. X
  1298. Xstatic void
  1299. Xinsert_op_2 (op, there, current_end, num_1, num_2)
  1300. X     char op;
  1301. X     char *there, *current_end;
  1302. X     int num_1, num_2;
  1303. X{
  1304. X  register char *pfrom = current_end;        /* Copy from here...  */
  1305. X  register char *pto = current_end + 5;        /* ...to here.  */
  1306. X
  1307. X  while (pfrom != there)                   
  1308. X    *--pto = *--pfrom;
  1309. X  
  1310. X  there[0] = op;
  1311. X  STORE_NUMBER (there + 1, num_1);
  1312. X  STORE_NUMBER (there + 3, num_2);
  1313. X}
  1314. X
  1315. X
  1316. X
  1317. X/* Given a pattern, compute a fastmap from it.  The fastmap records
  1318. X   which of the (1 << BYTEWIDTH) possible characters can start a string
  1319. X   that matches the pattern.  This fastmap is used by re_search to skip
  1320. X   quickly over totally implausible text.
  1321. X
  1322. X   The caller must supply the address of a (1 << BYTEWIDTH)-byte data 
  1323. X   area as bufp->fastmap.
  1324. X   The other components of bufp describe the pattern to be used.  */
  1325. X
  1326. Xvoid
  1327. Xre_compile_fastmap (bufp)
  1328. X     struct re_pattern_buffer *bufp;
  1329. X{
  1330. X  unsigned char *pattern = (unsigned char *) bufp->buffer;
  1331. X  int size = bufp->used;
  1332. X  register char *fastmap = bufp->fastmap;
  1333. X  register unsigned char *p = pattern;
  1334. X  register unsigned char *pend = pattern + size;
  1335. X  register int j, k;
  1336. X  unsigned char *translate = (unsigned char *) bufp->translate;
  1337. X
  1338. X  unsigned char *stackb[NFAILURES];
  1339. X  unsigned char **stackp = stackb;
  1340. X
  1341. X  unsigned is_a_succeed_n;
  1342. X
  1343. X  bzero (fastmap, (1 << BYTEWIDTH));
  1344. X  bufp->fastmap_accurate = 1;
  1345. X  bufp->can_be_null = 0;
  1346. X      
  1347. X  while (p)
  1348. X    {
  1349. X      is_a_succeed_n = 0;
  1350. X      if (p == pend)
  1351. X    {
  1352. X      bufp->can_be_null = 1;
  1353. X      break;
  1354. X    }
  1355. X#ifdef SWITCH_ENUM_BUG
  1356. X      switch ((int) ((enum regexpcode) *p++))
  1357. X#else
  1358. X      switch ((enum regexpcode) *p++)
  1359. X#endif
  1360. X    {
  1361. X    case exactn:
  1362. X      if (translate)
  1363. X        fastmap[translate[p[1]]] = 1;
  1364. X      else
  1365. X        fastmap[p[1]] = 1;
  1366. X      break;
  1367. X
  1368. X        case begline:
  1369. X        case before_dot:
  1370. X    case at_dot:
  1371. X    case after_dot:
  1372. X    case begbuf:
  1373. X    case endbuf:
  1374. X    case wordbound:
  1375. X    case notwordbound:
  1376. X    case wordbeg:
  1377. X    case wordend:
  1378. X          continue;
  1379. X
  1380. X    case endline:
  1381. X      if (translate)
  1382. X        fastmap[translate['\n']] = 1;
  1383. X      else
  1384. X        fastmap['\n'] = 1;
  1385. X            
  1386. X      if (bufp->can_be_null != 1)
  1387. X        bufp->can_be_null = 2;
  1388. X      break;
  1389. X
  1390. X    case jump_n:
  1391. X        case finalize_jump:
  1392. X    case maybe_finalize_jump:
  1393. X    case jump:
  1394. X    case dummy_failure_jump:
  1395. X          EXTRACT_NUMBER_AND_INCR (j, p);
  1396. X      p += j;    
  1397. X      if (j > 0)
  1398. X        continue;
  1399. X          /* Jump backward reached implies we just went through
  1400. X         the body of a loop and matched nothing.
  1401. X         Opcode jumped to should be an on_failure_jump.
  1402. X         Just treat it like an ordinary jump.
  1403. X         For a * loop, it has pushed its failure point already;
  1404. X         If so, discard that as redundant.  */
  1405. X
  1406. X          if ((enum regexpcode) *p != on_failure_jump
  1407. X          && (enum regexpcode) *p != succeed_n)
  1408. X        continue;
  1409. X          p++;
  1410. X          EXTRACT_NUMBER_AND_INCR (j, p);
  1411. X          p += j;        
  1412. X          if (stackp != stackb && *stackp == p)
  1413. X            stackp--;
  1414. X          continue;
  1415. X      
  1416. X        case on_failure_jump:
  1417. X    handle_on_failure_jump:
  1418. X          EXTRACT_NUMBER_AND_INCR (j, p);
  1419. X          *++stackp = p + j;
  1420. X      if (is_a_succeed_n)
  1421. X            EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  1422. X      continue;
  1423. X
  1424. X    case succeed_n:
  1425. X      is_a_succeed_n = 1;
  1426. X          /* Get to the number of times to succeed.  */
  1427. X          p += 2;        
  1428. X      /* Increment p past the n for when k != 0.  */
  1429. X          EXTRACT_NUMBER_AND_INCR (k, p);
  1430. X          if (k == 0)
  1431. X        {
  1432. X              p -= 4;
  1433. X              goto handle_on_failure_jump;
  1434. X            }
  1435. X          continue;
  1436. X          
  1437. X    case set_number_at:
  1438. X          p += 4;
  1439. X          continue;
  1440. X
  1441. X        case start_memory:
  1442. X    case stop_memory:
  1443. X      p++;
  1444. X      continue;
  1445. X
  1446. X    case duplicate:
  1447. X      bufp->can_be_null = 1;
  1448. X      fastmap['\n'] = 1;
  1449. X    case anychar:
  1450. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1451. X        if (j != '\n')
  1452. X          fastmap[j] = 1;
  1453. X      if (bufp->can_be_null)
  1454. X        return;
  1455. X      /* Don't return; check the alternative paths
  1456. X         so we can set can_be_null if appropriate.  */
  1457. X      break;
  1458. X
  1459. X    case wordchar:
  1460. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1461. X        if (SYNTAX (j) == Sword)
  1462. X          fastmap[j] = 1;
  1463. X      break;
  1464. X
  1465. X    case notwordchar:
  1466. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1467. X        if (SYNTAX (j) != Sword)
  1468. X          fastmap[j] = 1;
  1469. X      break;
  1470. X
  1471. X#ifdef emacs
  1472. X    case syntaxspec:
  1473. X      k = *p++;
  1474. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1475. X        if (SYNTAX (j) == (enum syntaxcode) k)
  1476. X          fastmap[j] = 1;
  1477. X      break;
  1478. X
  1479. X    case notsyntaxspec:
  1480. X      k = *p++;
  1481. X      for (j = 0; j < (1 << BYTEWIDTH); j++)
  1482. X        if (SYNTAX (j) != (enum syntaxcode) k)
  1483. X          fastmap[j] = 1;
  1484. X      break;
  1485. X#endif /* not emacs */
  1486. X
  1487. X    case charset:
  1488. X      for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1489. X        if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  1490. X          {
  1491. X        if (translate)
  1492. X          fastmap[translate[j]] = 1;
  1493. X        else
  1494. X          fastmap[j] = 1;
  1495. X          }
  1496. X      break;
  1497. X
  1498. X    case charset_not:
  1499. X      /* Chars beyond end of map must be allowed */
  1500. X      for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  1501. X        if (translate)
  1502. X          fastmap[translate[j]] = 1;
  1503. X        else
  1504. X          fastmap[j] = 1;
  1505. X
  1506. X      for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  1507. X        if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  1508. X          {
  1509. X        if (translate)
  1510. X          fastmap[translate[j]] = 1;
  1511. X        else
  1512. X          fastmap[j] = 1;
  1513. X          }
  1514. X      break;
  1515. X    }
  1516. X
  1517. X      /* Get here means we have successfully found the possible starting
  1518. X         characters of one path of the pattern.  We need not follow this
  1519. X         path any farther.  Instead, look at the next alternative
  1520. X         remembered in the stack.  */
  1521. X   if (stackp != stackb)
  1522. X    p = *stackp--;
  1523. X      else
  1524. X    break;
  1525. X    }
  1526. X}
  1527. END_OF_FILE
  1528. if test 45472 -ne `wc -c <'regex.c1'`; then
  1529.     echo shar: \"'regex.c1'\" unpacked with wrong size!
  1530. fi
  1531. # end of 'regex.c1'
  1532. fi
  1533. if test -r regex.c1 -a -r regex.c2
  1534. then
  1535.     echo shar: concatenating \"regex.c1\" and \"regex.c2\" into \"regex.c\"
  1536.     cat regex.c1 regex.c2 >regex.c || echo shar: \"regex.c\" creation failed!
  1537. fi
  1538. echo shar: End of archive 7 \(of 8\).
  1539. cp /dev/null ark7isdone
  1540. MISSING=""
  1541. for I in 1 2 3 4 5 6 7 8 ; do
  1542.     if test ! -f ark${I}isdone ; then
  1543.     MISSING="${MISSING} ${I}"
  1544.     fi
  1545. done
  1546. if test "${MISSING}" = "" ; then
  1547.     echo You have unpacked all 8 archives.
  1548.     rm -f ark[1-9]isdone
  1549. else
  1550.     echo You still need to unpack the following archives:
  1551.     echo "        " ${MISSING}
  1552. fi
  1553. ##  End of shell archive.
  1554. exit 0
  1555.  
  1556. exit 0 # Just in case...
  1557.